#include <iostream>
#include <queue>
#include <sstream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

string ans = "";

void pre(TreeNode* root) {
    if (!root) {
        ans += "#,";
    } else {
        ans += to_string(root->val);
        ans += ',';
        pre(root->left);
        pre(root->right);
    }
}

TreeNode* build(queue<string>& nodes) {
    string fr = nodes.front();
    nodes.pop();
    if (fr == "#") {
        return nullptr;
    } else {
        TreeNode* root = new TreeNode(stoi(fr));
        root->left = build(nodes);
        root->right = build(nodes);
        return root;
    }
}

string serialize(TreeNode* root) {
    pre(root);
    return ans;
}

TreeNode* deserialize(string data) {
    queue<string> nodes;
    stringstream sstr(data);
    string token;
    while (getline(sstr, token, ',')) { nodes.push(token); }
    return build(nodes);
}